package com.ddone.controller;

import com.ddone.enums.Log;
import com.ddone.enums.OperateType;
import com.ddone.enums.OperatorType;
import com.ddone.model.vo.AssignRoleVo;
import com.ddone.model.vo.Result;
import com.ddone.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author ddone
 * @date 2024/3/29-18:18
 */
@RequestMapping("/sys-user-role")
@RestController
@Api(tags = "用户角色")
public class SysUserRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @GetMapping("/{userId}")
    @ApiOperation("按照用户ID获取用户角色")
    public Result<Object> getRoleByUser(@ApiParam("用户ID") @PathVariable String userId){
        Map<String,Object> userRoleMap = sysRoleService.getRolesByUserId(userId);
        return Result.ok(userRoleMap);
    }

    @Log(title = "分配角色",operatorType = OperatorType.MANAGE,operateType = OperateType.INSERT,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation("分配角色")
    @PostMapping
    public Result<Object> assignRole(@RequestBody AssignRoleVo assignRoleVo){
        sysRoleService.doAssign(assignRoleVo);
        return Result.ok();
    }

}
